<html>
<head>
  <title>函数与操作</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/307027 (zh-CN, DDL); Windows/6.1.0 (Win32);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="651"/>
<h1>函数与操作</h1>

<div>
<span><div><div style="text-align: center;"><font color="#1C3387" style="font-size: 24pt;"><span style="font-size: 24pt; color: rgb(28, 51, 135); font-weight: bold;">Saver的用法</span></font></div><div style="text-align: center;"><br/></div><div><span style="font-size: 18pt; color: rgb(28, 51, 135);">Saver的背景介绍</span></div><div>         我们经常在训练完一个模型之后希望保存训练的结果，这些结果指的是<span style="font-size: 12pt; color: rgb(173, 0, 0);">模型的参数</span>，以<font style="font-size: 12pt;"><span style="font-size: 12pt; color: rgb(173, 0, 0); font-weight: bold;">便下次迭代的训练或者用作测试</span></font>。Tensorflow针对这一需求提供了<a href="https://www.tensorflow.org/versions/r0.9/api_docs/python/state_ops.html#Saver">Saver</a>类。</div><ol><li><span style="font-size: 18px; font-family: 'Courier New';">Saver类提供了</span><span style="font-size: 18px; color: rgb(173, 0, 0); font-family: 'Courier New'; font-weight: bold;">向checkpoints文件保存</span><span style="font-size: 18px; font-family: 'Courier New';">和</span><span style="font-size: 18px; color: rgb(173, 0, 0); font-family: 'Courier New'; font-weight: bold;">从checkpoints文件中恢复变量</span><span style="font-size: 18px; font-family: 'Courier New';">的相关方法。</span><span style="font-size: 18px; font-family: 'Courier New';">Checkpoints文件是一个二进制文件，它把</span><span style="font-size: 18px; color: rgb(50, 135, 18); font-family: 'Courier New'; font-weight: bold;">变量名映射到对应的tensor值</span> <span style="font-size: 18px; font-family: 'Courier New';">。</span></li><li><span style="font-size: 18px; font-family: 'Courier New';">只要提供一个计数器，当</span><span style="font-size: 18px; color: rgb(173, 0, 0); font-family: 'Courier New'; font-weight: bold;">计数器触发</span><span style="font-size: 18px; font-family: 'Courier New';">时，</span><span style="font-size: 18px; color: rgb(173, 0, 0); font-family: 'Courier New'; font-weight: bold;">Saver类可以自动的生成checkpoint文件</span><span style="font-size: 18px; font-family: 'Courier New';">。这让我们可以在训练过程中保存多个中间结果。例如，我们</span><span style="font-size: 18px; color: rgb(173, 0, 0); font-family: 'Courier New'; font-weight: bold;">可以保存每一步训练的结果</span><span style="font-size: 18px; font-family: 'Courier New';">。</span></li><li><span style="font-size: 18px; font-family: 'Courier New';">为了避免填满整个磁盘，Saver可以自动的管理Checkpoints文件。例如，我们</span><span style="font-size: 18px; font-family: 'Courier New'; text-decoration: underline;">可以指定保存最近的N个Checkpoints文件</span><span style="font-size: 18px; font-family: 'Courier New';">。</span></li></ol><div><br/></div><h2><span style="font-size: 24px;"><span style="font-size: 24px; color: rgb(28, 51, 135); font-weight: normal;">Saver的实例</span></span></h2><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import tensorflow as tf</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy as np</span></div><div><br/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">x = tf.placeholder(tf.float32, shape=[None, 1])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">y = 4 * x + 4</span></div><div><br/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">w = tf.Variable(tf.random_normal([1], -1, 1))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">b = tf.Variable(tf.zeros([1]))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">y_predict = w * x + b</span></div><div><br/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">loss = tf.reduce_mean(tf.square(y - y_predict))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">optimizer = tf.train.GradientDescentOptimizer(0.5)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">train = optimizer.minimize(loss)</span></div><div><br/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">isTrain = False</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">train_steps = 100</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">checkpoint_steps = 50</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">checkpoint_dir = ''</span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); font-size: 9pt; color: rgb(50, 135, 18); font-family: Monaco; font-weight: bold;">saver = tf.train.Saver()  # defaults to saving</span> <span style="background-color: rgb(251, 250, 248); font-size: 9pt; color: rgb(173, 0, 0); font-family: Monaco; font-weight: bold;">all variables</span> <span style="background-color: rgb(251, 250, 248); font-size: 9pt; color: rgb(50, 135, 18); font-family: Monaco; font-weight: bold;">- in this case w and b</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">x_data = np.reshape(np.random.rand(10).astype(np.float32), (10, 1))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">with tf.Session() as sess:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    sess.run(tf.initialize_all_variables())</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    if isTrain:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">        for i in xrange(train_steps):</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">            sess.run(train, feed_dict={x: x_data})</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">            if (i + 1) % checkpoint_steps == 0: # 存储的条件判断</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">                saver.save(sess, checkpoint_dir + '</span><span style="font-size: 9pt;"><span style="background-color: rgb(251, 250, 248); font-size: 9pt; color: rgb(173, 0, 0); font-family: Monaco; font-weight: bold;">model.ckpt</span></span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">', global_step=i+1)  # 文件名</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    else:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">        ckpt = tf.train.get_checkpoint_state(checkpoint_dir)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">        if ckpt and ckpt.model_checkpoint_path:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">            saver.restore(sess, ckpt.model_checkpoint_path)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">        else:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">            pass</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">       </span><span style="background-color: rgb(251, 250, 248); font-size: 10pt; color: rgb(173, 0, 0); font-family: Monaco;"> print(sess.run(w))</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 10pt; color: rgb(173, 0, 0); font-family: Monaco;">        print(sess.run(b))  # restore后可以直接打印出来</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div></div><div><br/></div><div>isTrain：用来区分训练阶段和测试阶段，True表示训练，False表示测试</div><div>train_steps：表示训练的次数，例子中使用100</div><div>checkpoint_steps：表示训练多少次保存一下checkpoints，例子中使用50</div><div>checkpoint_dir：表示checkpoints文件的保存路径，例子中使用当前路径</div><div><br/></div><div><br/></div><div><span style="font-size: 18pt; color: rgb(28, 51, 135);">训练阶段</span></div><div><font style="font-size: 14pt;"><span style="font-size: 14pt; color: rgb(173, 0, 0); font-weight: bold;">使用Saver.save()方法保存模型：</span></font></div><ul><li>sess：表示当前会话，当前会话记录了<font style="font-size: 12pt;"><span style="font-size: 12pt; color: rgb(173, 0, 0); font-weight: bold;">当前的变量值</span></font></li><li>checkpoint_dir + 'model.ckpt'：表示存储的文件名</li><li>global_step：表示当前是第几步</li></ul><div>训练完成后，当前目录底下会多出5个文件。</div><div><img src="函数与操作_files/Image.png" type="image/png" data-filename="Image.png" width="600"/></div><div><br/></div><div><br/></div><div><span style="font-size: 18pt; color: rgb(28, 51, 135);">测试阶段</span></div><div><br/></div><div>测试阶段使用saver.restore()方法恢复变量：</div><ol><li>sess：表示当前会话，之前保存的结果将被加载入这个会话</li><li>ckpt.model_checkpoint_path：表示模型存储的位置，不需要提供模型的名字，它会去查看checkpoint文件，看看<font style="font-size: 12pt;"><span style="font-size: 12pt; color: rgb(173, 0, 0); font-weight: bold;">最新的是谁</span></font>，叫做什么。</li></ol><div>    运行结果如下图所示，加载了之前训练的参数w和b的结果</div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div style="text-align: center;"><font color="#1C3387" style="font-size: 24pt;"><span style="font-size: 24pt; color: rgb(28, 51, 135); font-weight: bold;">TensorBoard</span></font></div><div style="text-align: center;"><br/></div><div><br/></div><div><span style="font-size: 18pt; color: rgb(28, 51, 135);">创建一个事件</span></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">writer = tf.summary.FileWriter('</span><span style="font-size: 9pt;"><span style="background-color: rgb(251, 250, 248); font-size: 9pt; color: rgb(173, 0, 0); font-family: Monaco; font-weight: bold;">./graphs</span></span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">, sess.graph) # route to keep your event files</span></div></div><div><br/></div><div>Go to terminal, run:</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt; python [yourprogram].py</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">&gt; tensorboard --logdir=./graphs --port 6006</span></div><div><br/></div></div><div>Then open your browser and go to : <a href="http://localhost:6006/">http://localhost:6006/</a></div><div><font style="font-size: 14pt;"><span style="font-size: 14pt; color: rgb(173, 0, 0); font-weight: bold;">注意：路径名不需用引号来表示</span></font></div><div><span style="font-size: 19px;"><span style="font-size: 19px; color: rgb(173, 0, 0); font-weight: bold; line-height: 27px;">注意二： 命令行中的路径和代码中的路径是一致的</span></span></div><div><br/></div><div><span style="font-size: 18pt; color: rgb(28, 51, 135);">Explicitly  name them</span></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">a = tf.constant(2, name=&quot;a&quot;)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">b = tf.constant(3, name=&quot;b&quot;)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">x = tf.add(a, b, name = &quot;add&quot;)</span></div></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div style="text-align: center;"><font style="font-size: 24pt;"><span style="font-size: 24pt; color: rgb(28, 51, 135); font-weight: bold;">tf.constant and tf.Variable</span></font></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div style="text-align: center;"><font style="font-size: 24pt;"><span style="font-size: 24pt; color: rgb(28, 51, 135); font-weight: bold;">tf.placeholder and feed_dict</span></font></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div style="text-align: center;"><font style="font-size: 24pt;"><span style="font-size: 24pt; color: rgb(28, 51, 135); font-weight: bold;">List of optimizers in TF</span></font></div><div><br/></div><div><ul><li>tf.train.GradientDescentOptimizer</li><li>tf.train.AdagradOptimizer</li><li>tf.train.MomentumOptimizer</li><li>tf.train.AdamOptimizer</li><li>tf.train.ProximalGradientDescentOptimizer</li><li>tf.train.ProximalAdagradOptimizer</li><li>tf.train.PMSPropOptimizer</li></ul></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div style="text-align: center;"><font style="font-size: 24pt;"><span style="font-size: 24pt; color: rgb(28, 51, 135); font-weight: bold;">Huber loss</span></font></div><div><br/></div><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">def huber_loss(labels, predictions, delta=1.0):</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    residual = tf.abs(predictions - labels)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    condition = tf.less(residual, delta)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    small_res = 0.5 * tf.square(residual)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    large_res = delta * residual -0.5 * tf.square(delta)</span></div><div><span style="background-color: rgb(251, 250, 248); font-size: 12px; color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">    return tf.select(condition, small_res, large_res)</span></div></div><div><img src="函数与操作_files/Image [1].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div style="text-align: center;"><font style="font-size: 24pt;"><span style="color: rgb(28, 51, 135); font-size: 24pt; font-weight: bold;">TRAIN</span></font></div><div><br/></div><div><img src="函数与操作_files/Image [2].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">with tf.Session as sess:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    # to visualize using tensorboard</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    writer = tf.summary.FileWriter('./graphs/logistic_reg',sess.graph)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    </span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    start_time = time.time()</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    sess.run(tf.global_variables_initializer())</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    n_batches = int(mnist.train.num_examples/bathc_size)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    for i in range(n_epochs): # train the model n_epochs times</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">        total_loss = 0</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    </span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">        for _ in range(n_batches):  </span><font style="font-size: 12pt;"><span style="background-color: rgb(251, 250, 248); color: rgb(173, 0, 0); font-family: Monaco; font-size: 12pt; font-weight: bold;"># n_batches 和 batch_size 组合会遍历55000个数据</span></font></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">            X_batch, Y_batch = mnist.train.next_batch(batch_size)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">          </span><font style="font-size: 12pt;"><span style="background-color: rgb(251, 250, 248); color: rgb(173, 0, 0); font-family: Monaco; font-size: 12pt; font-weight: bold;">  # 一个batch才会进行一次方向传播迭代</span></font></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">            _, loss_batch =sess.run([optimizer, loss], feed_dict={X:X_batch, Y:Y_batch})</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">            total_loss += loss_batch</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">        print('Average loss epoch {0}: {1}'.format(i, total_loss/n_batches))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    </span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    print('Total time: {0} seconds'.format(time.time() - start_time))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    print('Optimization Finished!')</span></div></div><div><br/></div></div><div><br/></div><div><br/></div><div style="text-align: center;"><font style="font-size: 24pt;"><span style="color: rgb(28, 51, 135); font-size: 24pt; font-weight: bold;">TEST</span></font></div><div style="text-align: center;"><div><br/></div></div><div style="text-align: left;"><br/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><br/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    #test the model</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    </span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    preds = tf.nn.softmax(logits)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(Y, 1))  # 两个向量中equal的项</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    </span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    n_batches = int(mnist.test.num_examples/bathc_size)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    total_correct_preds = 0</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    </span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    for i in range(n_batches):</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">        X_batch, Y_batch = mnist.test.next_batch(batch_size)       # 得到test 数据集</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">        accuracy_batch = sess.run([accuracy], feed_dict={X:X_batch, Y:Y_batch})</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">        total_correct_preds += accuracy_batch</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">    print('Accuracy {0}'.format(total_correct_preds/mnist.test.num_examples))</span></div></div><div style="text-align: left;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div><div style="text-align: center;"><br/></div></span>
</div></body></html> 